import { GCellRenderer } from "../../../../com/mui/controls/GCellRenderer";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { MailEventName } from "./MailEventName";
import { SMail } from "../../../../Message/Game/SMail";
import { ObjCreate } from "../common/ObjCreate";
import { GCheckBox } from "../../../../com/mui/controls/GCheckBox";
import { GLabel } from "../../../../com/mui/controls/GLabel";
import { ToolTipItem } from "../../../../com/mui/manager/ToolTipItem";
import { HTMLUtil } from "../../../../com/gengine/utils/HTMLUtil";
import { Language } from "../../../../extend/language/Language";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { DataEvent } from "../../events/DataEvent";
import { DateParser } from "../../../common/tools/DateParser";
import { EMailStatus } from "../../../../Message/Game/EMailStatus";
import { LoaderHelp } from "../../../common/display/LoaderHelp";
import { ResFileConst } from "../../resource/ResFileConst";
import { ImagesConst } from "../../resource/ImagesConst";
import { EventName } from "../../mvc/EventName";
import { GButton } from "../../../../com/mui/controls/GButton";
import { _SMail } from "../../../../this/_SMail";
type int = number;
//class MailRender
    
    export  class MailRender extends GCellRenderer
    {
        constructor()
        {
            super();
            this.createChildren();
            this.addEventListener(flash.events.MouseEvent.CLICK, this.onMailClickHandler);
            Dispatcher.addEventListener(MailEventName.SELALL_CLICK, this.onSelAllClickHandler);
            addEventListener(flash.events.MouseEvent.MOUSE_OVER, this.onMouseOver);
            addEventListener(flash.events.MouseEvent.MOUSE_OUT, this.onMouseOut);
            _selected = false;
            return;
        }

        public /* override */ set data(arg1: Object)
        {
            super.data = arg1;
            this._SMail = arg1["smail"] as SMail;
            this.index = arg1["index"] as int;
            if (this._SMail) 
            {
                this.update(this._SMail);
            }
            return;
        }

        /* internal  */createChildren(): void
        {
            ObjCreate.createBg(0, 0, 277, 48, this);
            this._checkBox = new GCheckBox();
            this._checkBox.label = "";
            this._checkBox.styleName = "GCheckBox";
            this._checkBox.move(11, 2);
            this._checkBox.drawNow();
            this._checkBox.addEventListener(flash.events.MouseEvent.CLICK, this.clickCheckBox);
            this.addChild(this._checkBox);
            this._mailTypeLabel = new GLabel();
            this._mailTypeLabel.width = 100;
            this._mailTypeLabel.height = 20;
            this._mailTypeLabel.move(40, 2);
            this._mailTypeLabel.mouseEnabled = false;
            this._mailTypeLabel.mouseChildren = false;
            this.addChild(this._mailTypeLabel);
            this._mailDateLabel = ObjCreate.createTextField("", 78, 3, 100, 20, this);
            this._mailDateLabel.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._mailDateLabel.mouseEnabled = false;
            this._mailStatusLabel = new GLabel();
            this._mailStatusLabel.width = 100;
            this._mailStatusLabel.height = 20;
            this._mailStatusLabel.move(145, 3);
            this._mailStatusLabel.mouseEnabled = false;
            this._mailStatusLabel.mouseChildren = false;
            this.addChild(this._mailStatusLabel);
            this._readStatusBitmap = new Object /* flash.display.Bitmap */();
            this._readStatusBitmap.x = 195;
            this._readStatusBitmap.y = 3;
            this.addChild(this._readStatusBitmap);
            this._attItem = new ToolTipItem();
            this._attItem.x = 215;
            this._attItem.y = 5;
            this._attItem.visible = false;
            this._attItem.buttonMode = true;
            this._attItem.useHandCursor = true;
            this._attItem.toolTipData = HTMLUtil.addColor(Language.getString(52018), "#ffffff");
            this._attItem.addEventListener(flash.events.MouseEvent.CLICK, this.onAttItemClickHandler);
            this.addChild(this._attItem);
            this._mailAttachmentBitmap = new Object /* flash.display.Bitmap */();
            this._attItem.addChild(this._mailAttachmentBitmap);
            this._mailTitleLabel = new GLabel();
            this._mailTitleLabel.x = 38;
            this._mailTitleLabel.y = 25;
            this._mailTitleLabel.height = 25;
            this._mailTitleLabel.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this.addChild(this._mailTitleLabel);
            this._overSkin = GlobalClass.getBitmap("MailreadAcross");
            this._overSkin.x = 45;
            this._getAttachmentBtn = ObjCreate.createGButton(Language.getString(52062), 230, 5, 40, 22, this);
            this._getAttachmentBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onAttItemClickHandler);
            this._getAttachmentBtn.drawNow();
            return;
        }

        public getToolTipData(arg1=null)
        {
            return;
        }

        public onMailClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(MailEventName.MAIL_CLICK, {"smail": _SMail, "mailRender": this}));
            this._SMail.status = 2;
            this.update(this._SMail);
            return;
        }

        public onSelAllClickHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Boolean;
            this._checkBox.selected = loc1;
            this.data["isSelected"] = loc1;
            return;
        }

        public clickCheckBox(arg1: Object /* flash.events.Event */): void
        {
            if (this._checkBox.selected) 
            {
                this.data["isSelected"] = true;
                Dispatcher.dispatchEvent(new DataEvent(MailEventName.MAIL_SELECTED, this._SMail.mailId));
            }
            else 
            {
                this.data["isSelected"] = false;
                Dispatcher.dispatchEvent(new DataEvent(MailEventName.MAIL_UNSELECTED, this._SMail.mailId));
            }
            return;
        }

        public update(arg1: SMail): void
        {
            if (arg1.type != 1) 
            {
                this._mailTypeLabel.htmlText = HTMLUtil.addColor(Language.getString(52054), "#FF3B3D");
            }
            else 
            {
                this._mailTypeLabel.htmlText = HTMLUtil.addColor(Language.getString(52019), "#FF3B3D");
            }
            var loc1="";
            loc1 = DateParser.parse(arg1.mailDt, "MM-dd hh: mm");
            this._mailDateLabel.text = loc1;
            if (arg1.status != EMailStatus._EMailStatusUnRead) 
            {
                if (arg1.status == EMailStatus._EMailStatusRead) 
                {
                    this._mailStatusLabel.htmlText = HTMLUtil.addColor(Language.getString(52021), "#F4FF00");
                }
            }
            else 
            {
                this._mailStatusLabel.htmlText = HTMLUtil.addColor(Language.getString(52020), "#FF3B3D");
            }
            if (arg1.hadAttachment) 
            {
                this._attItem.visible = true;
                this._getAttachmentBtn.visible = true;
            }
            else 
            {
                this._attItem.visible = false;
                this._getAttachmentBtn.visible = false;
            }
            this._mailTitleLabel.htmlText = HTMLUtil.addColor(arg1.title.substring(0, 19), "#B1F0FF");
            LoaderHelp.addResCallBack(ResFileConst.mail, this.updateBitmap);
            return;
        }

        /* internal  */updateBitmap(): void
        {
            if (this._SMail.status != EMailStatus._EMailStatusUnRead) 
            {
                if (this._SMail.status == EMailStatus._EMailStatusRead) 
                {
                    this._readStatusBitmap.bitmapData = GlobalClass.getBitmapData(ImagesConst.Read);
                }
            }
            else 
            {
                this._readStatusBitmap.bitmapData = GlobalClass.getBitmapData(ImagesConst.Unread);
            }
            if (this._SMail.hadAttachment) 
            {
                this._mailAttachmentBitmap.bitmapData = GlobalClass.getBitmapData(ImagesConst.Attachment);
            }
            else 
            {
                this._mailAttachmentBitmap.bitmapData = null;
            }
            return;
        }

        /* internal  */onMouseOver(arg1: Object /* flash.events.MouseEvent */): void
        {
            addChildAt(this._overSkin, 0);
            return;
        }

        /* internal  */onMouseOut(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (!_selected) 
            {
                if (this._overSkin.parent) 
                {
                    removeChild(this._overSkin);
                }
            }
            return;
        }

        public get checkBox(): GCheckBox
        {
            return this._checkBox;
        }

        public /* override */ set selected(arg1: Boolean)
        {
            super.selected = arg1;
            if (arg1) 
            {
                addChildAt(this._overSkin, 0);
            }
            else if (this._overSkin.parent) 
            {
                removeChild(this._overSkin);
            }
            return;
        }

        /* internal  */onAttItemClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._SMail) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.MailGetAttachment, {"mailId": this._SMail.mailId, "index": 0}));
            }
            return;
        }

        /* internal */ /* var */_bg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_checkBox: GCheckBox;

        /* internal */ /* var */_mailTypeLabel: GLabel;

        /* internal */ /* var */_mailDateLabel: Object /* flash.text.TextField */;

        /* internal */ /* var */_mailStatusLabel: GLabel;

        /* internal */ /* var */_mailTitleLabel: GLabel;

        /* internal */ /* var */_readStatusBitmap: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_attItem: ToolTipItem;

        /* internal */ /* var */_mailAttachmentBitmap: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_overSkin: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_getAttachmentBtn: GButton;

        /* internal */ /* var */_SMail: SMail;

        public /* var */index: int;
    }
